package com.atguigu.gmall.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author: 封捷
 * @create-date: 2022/8/29 15:26
 */
@Controller
public class PortalController {

    @Autowired
    private ProductFeignClient productFeignClient;

    // @RequestMapping 注解中指定两个值，表示这个方法和这两个值都匹配
    @RequestMapping({"/", "/index.html"})
    public String toPortalPage(Model model) {

        // 调用远程接口
        Result<List<JSONObject>> result = productFeignClient.getCategoryForPortal();

        // 从远程调用结果中获取数据
        List<JSONObject> categoryJSONData = result.getData();

        // 将 JSON 数据存入模型
        model.addAttribute("list", categoryJSONData);

        // 返回首页对应的逻辑视图页面
        return "index/index";
    }

}
